package cn.fufeiii.bt.nomiddleware.openfeign;

import cn.fufeiii.bt.common.util.result.BizException;
import cn.fufeiii.bt.nomiddleware.openfeign.api.StudentApi;
import cn.fufeiii.bt.nomiddleware.openfeign.dto.AddRequest;
import cn.fufeiii.bt.nomiddleware.openfeign.dto.GetListRequest;
import cn.fufeiii.bt.nomiddleware.openfeign.dto.GetListResponse;
import cn.fufeiii.bt.nomiddleware.openfeign.dto.GetResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 伪装一个外部的服务端 将会由StudentApiFeignClient来调用
 *
 * @author FuFei
 */
@Slf4j
@RestController
@RequestMapping(StudentApiFeignClientController.BASE_PATH)
public class StudentApiFeignClientController implements StudentApi {

    public static final String BASE_PATH = "/rest/client/api";

    @PostMapping(PATH_ADD)
    @Override
    public void add(@RequestBody AddRequest addRequest) {
        log.info("当前线程{},请求内容{}", Thread.currentThread().getName(), addRequest);
    }

    @GetMapping(PATH_GET)
    @Override
    public GetResponse get(@PathVariable Long id) {
        log.info("当前线程{},请求内容{}", Thread.currentThread().getName(), id);
        if (id % 2 == 0) {
            throw new BizException("业务出现问题");
        }
        GetResponse getResponse = new GetResponse();
        getResponse.setId(id);
        return getResponse;
    }


    @PostMapping(PATH_GET_LIST)
    @Override
    public List<GetListResponse> getList(@RequestBody GetListRequest request) {
        log.info("当前线程{},请求内容{}", Thread.currentThread().getName(), request);
        if (request != null && request.getId() % 2 == 0) {
            throw new BizException("业务出现问题");
        }
        GetListResponse getListResponse = new GetListResponse();
        getListResponse.setId(request.getId());
        return List.of(getListResponse);
    }

}
